/*******************************************************************************
 * Copyright (c) 2012 Alexandr Tsvetkov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Alexandr Tsvetkov - initial API and implementation
 * 
 * Project:
 *     TAO Library
 * 
 * File name:
 *     ActivityStack.java
 *     
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 ******************************************************************************/
package ua.at.tsvetkov.application;

import java.util.HashSet;

import ua.at.tsvetkov.util.Log;

import android.app.Activity;

/**
 * You Activities Manager
 *
 * @author Tsvetkov A.O. 2011. Email al@ukr.net
 */
public class ActivityStack {

	private static HashSet<Activity>	activities	= new HashSet<Activity>();
	private static Activity				lastActivity;

	/**
	 * Stop all Activities except current
	 *
	 * @param current
	 */
	public static void stopAllexceptMe(Activity current) {
		for (Activity activity : activities) {
			if (activity != null && activity != current) {
				activity.finish();
			}
		}
		activities.clear();
		activities.add(current);
	}

	/**
	 * Add current Activity to stack. Must be called in oCreate method.
	 *
	 * @param current
	 */
	public static void add(Activity current) {
		lastActivity = current;
		activities.add(current);
	}

	/**
	 * Remove Activity from stack. Must be called in finish() method.
	 *
	 * @param current
	 */
	public static void remove(Activity activity) {
		lastActivity = null;
		activities.remove(activity);
	}

	/**
	 * Clear Activity stack
	 */
	public static void clear() {
		activities.clear();
	}

	/**
	 * Return last Activity added to stack.
	 *
	 * @return
	 */
	public static Activity getLastActivity() {
		return lastActivity;
	}

	/**
	 * Check is null last Activity
	 *
	 * @return
	 */
	public static boolean isEmpty() {
		boolean result = lastActivity == null;
		if (result)
			Log.w("ActivityStack is empty, current Activity == null. Initialise it first!");
		return result;
	}

}
